package com.cj.library.widget.view.loadingview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;

import com.cj.library.widget.R;
import com.cj.library.widget.utiles.DensityUtils;

/**
 * Created by peter on 2016/9/21.
 */

public class RotateBallLodingView extends CJLodingView {

    private Paint paint;
    private int rotateAngle = 0;
    private int stepAngle;
    private int ballCount;
    private float maxBallR;
    private int startColor;
    private int endColor;

    public RotateBallLodingView(Context context) {
        super(context);
    }

    public RotateBallLodingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RotateBallLodingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs) {
        super.init(context, attrs);
        TypedArray types = context.obtainStyledAttributes(attrs, R.styleable.CJLodingView_RotateBallLodingView);
        stepAngle = types.getInt(R.styleable.CJLodingView_RotateBallLodingView_stepAngle, 3);
        ballCount = types.getInt(R.styleable.CJLodingView_RotateBallLodingView_ballCount, 12);
        maxBallR = types.getDimension(R.styleable.CJLodingView_RotateBallLodingView_maxBallR, 25);
        startColor = types.getColor(R.styleable.CJLodingView_RotateBallLodingView_startColor, Color.BLUE);
        endColor = types.getColor(R.styleable.CJLodingView_RotateBallLodingView_endColor, Color.GREEN);
        types.recycle();
    }

    @Override
    protected void drawIml(Canvas canvas) {

        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();


        float cellAngle = 360.0f / ballCount;

        canvas.save();
        canvas.rotate(rotateAngle, w / 2, h / 2);
        for (int i = 0; i < ballCount; i++) {
            canvas.drawCircle(w / 2, maxBallR + getPadding(), maxBallR * (i + 1) / ballCount, mPaint);
            canvas.rotate(cellAngle, w / 2, h / 2);
        }
        canvas.restore();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = 0;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = 0;

        switch (widthMode) {
            case MeasureSpec.AT_MOST:
                widthSize = DensityUtils.dip2px(getContext(), 50);
                break;
            case MeasureSpec.EXACTLY:
                widthSize = MeasureSpec.getSize(widthMeasureSpec);
                break;
        }

        switch (heightMode) {
            case MeasureSpec.AT_MOST:
                widthSize = DensityUtils.dip2px(getContext(), 50);
                break;
            case MeasureSpec.EXACTLY:
                heightSize = MeasureSpec.getSize(heightMeasureSpec);
                break;
        }


        if (widthSize > heightSize) {
            heightSize = widthSize;
        }
        mPaint.setShader(new RadialGradient(widthSize / 2, widthSize / 2, widthSize / 2, startColor, endColor, Shader.TileMode.CLAMP));
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void animRunnableIml() {
        rotateAngle = (rotateAngle + stepAngle) % 360;
    }


}
